//
//  SISSHouseNumCheckViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/6/27.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSHouseNumVerifyViewController.h"
#import "SWForm.h"
#import "SISSHouseEnrollFirstViewController.h"
#import "SISSHouseNumVerifyErrorViewController.h"
#import "SISSJsonRequestNetworkingManager+Registration.h"
#import "SISSCheckHouseResultModel.h"

@interface SISSHouseNumVerifyViewController ()

@property (weak, nonatomic) SWFormItem *houseNumItem;
@property (weak, nonatomic) SWFormItem *roomItem;

@end

@implementation SISSHouseNumVerifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 添加左侧的关闭按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    [self initUI];
}

- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UI
- (void)initUI {
    NSMutableArray *items = [NSMutableArray array];
    
    SWFormItem *houseNumItem = [SWFormItem sw_itemWithTitle:@"房源编码" info:@"" itemType:SWFormItemTypeInput editable:YES required:YES keyboardType:UIKeyboardTypeDefault];
    houseNumItem.placeholder = @"请输入已获得委托的房源编码";
    [items addObject:houseNumItem];
    self.houseNumItem = houseNumItem;
    
    SWFormItem *roomItem = [SWFormItem sw_itemWithTitle:@"房号" info:@"" itemType:SWFormItemTypeInput editable:YES required:YES keyboardType:UIKeyboardTypeDefault];
   
    [items addObject:roomItem];
    self.roomItem = roomItem;
    
    SWFormSectionItem *sectionItem = [SWFormSectionItem sw_sectionItem:items];
    //    sectionItem.headerHeight = 10;
    [self.mutableItems addObject:sectionItem];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 66)];
    headView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.formTableView.tableHeaderView = headView;
    self.formTableView.tableFooterView = [self footerView];
    self.formTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
}

/**
 下一步按钮点击事件
 */
- (void)nextStepButtonClickAction {
    // 这里只是简单描述校验逻辑，可根据自身需求封装数据校验逻辑
    [SWFormHandler jyt_checkFormNullDataWithWithDatas:self.mutableItems success:^{
        
        NSLog(@"houseNumItem === %@", self.houseNumItem.info);
        NSLog(@"self.roomItem.info %@", self.roomItem.info);
        // TODO 网络请求
        NSString *roomNum = self.roomItem.info ? self.roomItem.info : @"";
        [[SISSJsonRequestNetworkingManager sharedManager] verifyHouseNumWithHouseNum:self.houseNumItem.info roomNum:roomNum successBlock:^(id responseObject) {
            
            if ([responseObject[@"code"] integerValue] == 200) {
                SISSHouseEnrollFirstViewController *vc = [[SISSHouseEnrollFirstViewController alloc] init];
                SISSCheckHouseResultModel *checkHouseModel = [SISSCheckHouseResultModel yy_modelWithJSON:responseObject[@"data"]];
                vc.isRentOut = self.isRentOut;
                vc.isRegistration = self.isRegistration;
                vc.isCooperation = self.isCooperation;
                vc.checkHouseModel = checkHouseModel;
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                SISSHouseNumVerifyErrorViewController *vc = [[SISSHouseNumVerifyErrorViewController alloc] init];
                vc.errorMsg = responseObject[@"data"];
                [self.navigationController pushViewController:vc animated:YES];
            }
        } failed:^(NSError *error) {
            
            NSLog(@"%@",error);
        }];
        
    } failure:^(NSString *error) {
        NSLog(@"error====%@",error);
        [self alertActionAlertWithTitle:@"提示" message:error];
    }];
}

/**
 创建footer
 */
- (UIView *)footerView {
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 160)];
    
    UILabel *label = [UILabel yyr_labelWithText:@"输入正确的房号将查询到更多信息" fontSize:16 textColor:MAIN_TEXT_COLOR];
    label.frame = CGRectMake(8, 0, SCREEN_WIDTH-32, 32);
    [footer addSubview:label];
    
    UIButton *nextStepButton = [UIButton buttonWithType:UIButtonTypeSystem];
    nextStepButton.bounds = CGRectMake(0, 66, SCREEN_WIDTH * 0.8, 44);
    nextStepButton.center = footer.center;
    nextStepButton.backgroundColor = MAIN_COLOR;
    nextStepButton.layer.cornerRadius = 8;
    nextStepButton.layer.masksToBounds = YES;
    [nextStepButton setTitle:@"下一步" forState:UIControlStateNormal];
    [nextStepButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextStepButton addTarget:self action:@selector(nextStepButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:nextStepButton];
    
    return footer;
}

@end
